﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PopupControl;
using System.Threading;
using System.Xml;
using System.IO;

namespace simpleGUI_QLQTPM
{
    public partial class Form_DanhSach : Form
    {
        public Form_DanhSach()
        {
            InitializeComponent();
        }


        private void LB_CapNhatDuLieu_MouseEnter(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseEnter(sender, e);
        }

        private void LB_CapNhatDuLieu_MouseLeave(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseLeave(sender, e);
        }

        private void Panel_CapNhatDuLieu_MouseEnter(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton2_hover;
        }

        private void Panel_CapNhatDuLieu_MouseLeave(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.staticbutton2;
        }

        private void PB_CapNhatDuLieu_MouseEnter(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseEnter(sender, e);
        }

        private void PB_CapNhatDuLieu_MouseLeave(object sender, EventArgs e)
        {
            Panel_CapNhatDuLieu_MouseLeave(sender, e);
        }

        private void PB_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;

            switch (pb.Name)
            {
                case "PB_TatCa":
                    PB_TatCa.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_tatca;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_tatca;
                    break;
                case "PB_ATM":
                    PB_ATM.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_atm;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_atm;
                    break;
                case "PB_CayXang":
                    PB_CayXang.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_cayxang;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_cayxang;
                    break;
                case "PB_BenhVien":
                    PB_BenhVien.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_benhvien;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_benhvien;
                    break;
                case "PB_NhaHang":
                    PB_NhaHang.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_nhahang;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_nhahang;
                    break;
                case "PB_SieuThi":
                    PB_SieuThi.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_sieuthi;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_sieuthi;
                    break;
                case "PB_NganHang":
                    PB_NganHang.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_nganhang;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_nganhang;
                    break;
                case "PB_BuuDien":
                    PB_BuuDien.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_buudien;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_buudien;
                    break;
                case "PB_RapChieuPhim":
                    PB_RapChieuPhim.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_rapchieuphim;
                    PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_rapchieuphim;
                    break;
                default:
                    pb.BackgroundImage = null;
                    break;
            }

        }

        private void PB_MouseLeave(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;

            PB_Description.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.descript_none;

            switch (pb.Name)
            {
                case "PB_TatCa":
                    PB_TatCa.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_tatca_hover;
                    break;
                case "PB_ATM":
                    PB_ATM.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_atm_hover;
                    break;
                case "PB_CayXang":
                    PB_CayXang.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_cayxang_hover;
                    break;
                case "PB_BenhVien":
                    PB_BenhVien.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_benhvien_hover;
                    break;
                case "PB_NhaHang":
                    PB_NhaHang.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_nhahang_hover;
                    break;
                case "PB_SieuThi":
                    PB_SieuThi.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_sieuthi_hover;
                    break;
                case "PB_NganHang":
                    PB_NganHang.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_nganhang_hover;
                    break;
                case "PB_BuuDien":
                    PB_BuuDien.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_buudien_hover;
                    break;
                case "PB_RapChieuPhim":
                    PB_RapChieuPhim.BackgroundImage = simpleGUI_QLQTPM.Properties.Resources.icon_rapchieuphim_hover;
                    break;
                default:
                    pb.BackgroundImage = null;
                    break;
            }
        }

        private void PB_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;

            string strDichVu = String.Empty;
            switch (pb.Name)
            {
                case "PB_TatCa":
                    strDichVu = @"Tất cả dịch vụ";
                    break;
                case "PB_ATM":
                    strDichVu = @"ATM";
                    break;
                case "PB_CayXang":
                    strDichVu = @"Trạm xăng";
                    break;
                case "PB_BenhVien":
                    strDichVu = @"Bệnh viện";
                    break;
                case "PB_NhaHang":
                    strDichVu = @"Nhà hàng";
                    break;
                case "PB_SieuThi":
                    strDichVu = @"Chợ";
                    break;
                case "PB_NganHang":
                    strDichVu = @"Ngân hàng";
                    break;
                case "PB_BuuDien":
                    strDichVu = @"Bưu điện";
                    break;
                case "PB_RapChieuPhim":
                    strDichVu = @"Rạp chiếu phim";
                    break;
            }

            Point location = this.Location;
            Form_TimKiem form = new Form_TimKiem(strDichVu);
            form.Location = location;
            form._parent = this;

            this.Visible = false;
            form.Show();
        }

        int WidthRate, HeightRate, LeftRate, TopRate;

        void Size_Rate(int width, int height, int left, int top)
        {
            WidthRate = (width * 10000) / 800;
            HeightRate = (height * 10000) / 600;
            LeftRate = (left * 10000) / 800;
            TopRate = (top * 10000) / 600;
        }

        void Resize_Control(Control TempControl)
        {
            TempControl.Width = (this.Width * WidthRate) / 10000;
            TempControl.Height = (this.Height * HeightRate) / 10000;
            TempControl.Left = (this.Width * LeftRate) / 10000;
            TempControl.Top = (this.Height * TopRate) / 10000;
        }

        private void Form_DanhSach_SizeChanged(object sender, EventArgs e)
        {
            Size_Rate(479, 74, 292, 33); Resize_Control(PB_Noti_Text);
            Size_Rate(48, 48, 239, 47); Resize_Control(PB_Noti_Icon);
            Size_Rate(186, 96, 0, 149); Resize_Control(Panel_CapNhatDuLieu);
            Size_Rate(155, 34, 242, 210); Resize_Control(pictureBox1);
            Size_Rate(155, 34, 432, 210); Resize_Control(pictureBox2);
            Size_Rate(155, 34, 621, 210); Resize_Control(pictureBox3);
            Size_Rate(155, 34, 242, 322); Resize_Control(pictureBox4);
            Size_Rate(155, 34, 432, 322); Resize_Control(pictureBox5);
            Size_Rate(155, 34, 621, 322); Resize_Control(pictureBox6);
            Size_Rate(155, 34, 242, 434); Resize_Control(pictureBox7);
            Size_Rate(155, 34, 432, 434); Resize_Control(pictureBox8);
            Size_Rate(155, 34, 621, 434); Resize_Control(pictureBox9);
            Size_Rate(90, 55, 277, 149); Resize_Control(PB_TatCa);
            Size_Rate(90, 55, 465, 149); Resize_Control(PB_ATM);
            Size_Rate(90, 55, 653, 149); Resize_Control(PB_CayXang);
            Size_Rate(90, 55, 277, 261); Resize_Control(PB_BenhVien);
            Size_Rate(90, 55, 465, 261); Resize_Control(PB_NhaHang);
            Size_Rate(90, 55, 653, 261); Resize_Control(PB_SieuThi);
            Size_Rate(90, 55, 277, 373); Resize_Control(PB_NganHang);
            Size_Rate(90, 55, 465, 373); Resize_Control(PB_BuuDien);
            Size_Rate(90, 55, 653, 373); Resize_Control(PB_RapChieuPhim);
            Size_Rate(576, 64, 208, 486); Resize_Control(PB_Description);
        }

        private void Form_DanhSach_Load(object sender, EventArgs e)
        {
            ToolTip tt = new ToolTip();
            tt.SetToolTip(this.PB_CapNhatDuLieu, "Cập nhật dữ liệu mới nhất");
            tt.SetToolTip(this.PB_TatCa, "Tìm kiếm dịch vụ bất kì");
            tt.SetToolTip(this.PB_ATM, "Tìm kiếm các máy ATM");
            tt.SetToolTip(this.PB_BenhVien, "Tìm kiếm các bệnh viện");
            tt.SetToolTip(this.PB_BuuDien, "Tìm kiếm các bưu điện");
            tt.SetToolTip(this.PB_CayXang, "Tìm kiếm các cây xăng");
            tt.SetToolTip(this.PB_NganHang, "Tìm kiếm các ngân hàng");
            tt.SetToolTip(this.PB_NhaHang, "Tìm kiếm các nhà hàng");
            tt.SetToolTip(this.PB_RapChieuPhim, "Tìm kiếm các rạp chiếu phim");
            tt.SetToolTip(this.PB_SieuThi, "Tìm kiếm các siêu thị");
        }
        /// <summary>
        /// tao file config neu file bi xoa
        /// </summary>
        private void createConfig()
        {
            XmlTextWriter writer = new XmlTextWriter("config.xml", Encoding.UTF8);
            writer.WriteStartDocument();
            writer.WriteStartElement("CONFIG");
            writer.WriteString("\n");
            writer.WriteStartElement("HELP");
            writer.WriteStartElement("OPEN");
            writer.WriteString("true");
            writer.WriteEndElement();//end HELP
            writer.WriteEndElement();//end OPEN
            writer.WriteStartElement("MAP");
            writer.WriteStartElement("PATH");
            writer.WriteString("BanDo");
            writer.WriteEndElement();//end MAP
            writer.WriteEndElement();//end PATH
            writer.WriteEndElement();//end config
            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();
        }

        /// <summary>
        /// read config xml file
        /// </summary>
        private void OpenConfigFile()
        {
            XmlDocument document = new XmlDocument();
            document.Load("config.xml");

            XmlElement root = document.DocumentElement;
            XmlElement element = (XmlElement)root.SelectSingleNode("//OPEN");

            string open = element.InnerText;
            if (open.Equals("true"))
            {
                Form_Help form = new Form_Help();
                form._parent = this;
                form.Show();
            }
        }

        /// <summary>
        /// Trang thai cua form khi mo chuong trinh
        /// </summary>
        private void XacDinhTrangThaiBanDau()
        {
            try
            {
                if (!File.Exists("config.xml"))
                {
                    createConfig();
                    OpenConfigFile();
                }
                else
                {
                    OpenConfigFile();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form_Help form = new Form_Help();
            form._parent = this;
            form.ShowDialog();
        }

        private void Form_DanhSach_Shown(object sender, EventArgs e)
        {
            XacDinhTrangThaiBanDau();
        }
    }
}
